import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:view_lamp/custom/view_lamp_item.dart';

import 'lamp_widget.dart';

///填充模式
///黄云音
///2020年3月1日
class CustomViewLamp3 extends StatefulWidget {
  final double multiColorWidth;
  final double multiColorHeight;
  final Color fillcolor;
  final Color bgcolor;

  const CustomViewLamp3({
    Key key,
    this.multiColorWidth,
    this.multiColorHeight, this.fillcolor, this.bgcolor,
  }) : super(key: key);

  @override
  _CustomViewLamp3State createState() => _CustomViewLamp3State();
}

class _CustomViewLamp3State extends State<CustomViewLamp3> {
  Timer timer;

  // List<Color> list =[];
  List<ViewLampItem> lampItemInfoList = [
    ViewLampItem(17, 72, Colors.transparent, 'assets/images/previewbulb1-x17y72.png'),
    ViewLampItem(82, 90, Colors.transparent, 'assets/images/previewbulb2-x82y90.png'),
    ViewLampItem(147, 104, Colors.transparent, 'assets/images/previewbulb3-x147y104.png'),
    ViewLampItem(213, 106, Colors.transparent, 'assets/images/previewbulb4-x213y106.png'),
    ViewLampItem(279, 101, Colors.transparent, 'assets/images/previewbulb5-x279y101.png'),
    ViewLampItem(344, 90, Colors.transparent, 'assets/images/previewbulb6-x344y90.png'),
    ViewLampItem(409, 73, Colors.transparent, 'assets/images/previewbulb7-x409y73.png'),
    ViewLampItem(474, 60, Colors.transparent, 'assets/images/previewbulb8-x474y60.png'),
    ViewLampItem(540, 60, Colors.transparent, 'assets/images/previewbulb9-x540y60.png'),
    ViewLampItem(606, 70, Colors.transparent, 'assets/images/previewbulb10-x606y70.png'),
    ViewLampItem(670, 87, Colors.transparent, 'assets/images/previewbulb11-x670y87.png'),
    ViewLampItem(736, 102, Colors.transparent, 'assets/images/previewbulb12-x736y102.png'),
  ];

  @override
  void initState() {
    super.initState();
    timer = Timer.periodic(Duration(seconds: 1), (Timer t) => colorValue());
  }

  void colorValue() {
    setState(() {
      if (lampItemInfoList.any((element) => element.color == widget.fillcolor)) {
        var needChangItems = lampItemInfoList.where((element) => element.color == widget.fillcolor).toList();
        int random = Random().nextInt(needChangItems.length);
        int secondRandom = Random().nextInt(needChangItems.length-1);
        if (secondRandom == random) {
          if (secondRandom == needChangItems.length) {
            secondRandom--;
          } else {
            secondRandom++;
          }
        }
        needChangItems[random].color = widget.bgcolor;
        needChangItems[secondRandom].color = widget.bgcolor;
      } else {
        lampItemInfoList.forEach((element) {
          element.color = widget.fillcolor;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return LightChainWidget(lampItemInfoList: lampItemInfoList, multiColorWidth: widget.multiColorWidth,multiColorHeight:widget.multiColorHeight);
  }

  @override
  void dispose() {
    timer?.cancel();
    super.dispose();
  }
}

